﻿@model IEnumerable<Superstore.Store.Models.MenuInfo>
@{
    int mainMenuFocus = Convert.ToInt32(ViewBag.mainMenuFocus);  //主菜单默认焦点
    int subMenuFocus = Convert.ToInt32(ViewBag.subMenuFocus);    //子菜单默认焦点
}
<!--商品类别-->
<section class="navWindow">
    <nav id="mainMenuList" class="IndexNav">
        <ul>
            @foreach (var item in Model)
            {
                if (item.sub_menu_list != null && item.sub_menu_list.Count > 0)
                {
                    <li class="IndexNavF" onclick="mainMenuClick(this, @item.type)"><a href="#">@item.name<img src="/images/IndexNav.png" alt=""></a></li>
                    //二级菜单
                    foreach (var subItem in item.sub_menu_list)
                    {
                        <li id="menuid_@subItem.type" class="IndexNavSon" onclick="getMenuGoodsInfo(@item.type, @subItem.type)"><a href="#">@subItem.name</a></li>
                    }
                }
                else
                {
                    <li id="menuid_@item.type" class="IndexNavF" onclick="getMenuGoodsInfo(@item.type, -1)"><a href="#">@item.name</a></li>
                }
            }
        </ul>
    </nav>
</section>
<!--商品列表-->
<section class="goodsWindow">
    <div id="goodsList" class="IndexGoods"></div>
</section>

<script>
    var scrollBegin = 0;

    $(function () {
        //当前浏览器窗口高度 考虑到header和购物车的高度 45 + 50 = 95
        var currentWindowHeight = $(window).height() - 95;
        var attrHeight = "height:" + currentWindowHeight + "px";
        //当前容器确定高度
        $(".IndexCon").attr("style", attrHeight);
        setClickMenu();
        setGoodsListScrollListener();
        scrollBegin = 0;
        GoodsMgr.getGoodsList(scrollBegin, GoodsMgr.goodsQueryNum, addGoodsList);
    });

    //function showMenuList() {
    //    var mainTab = TerminalMgr.getCurMainTab();
    //    var subTab = TerminalMgr.getCurSubTab();
    //    var menuList = TerminalMgr.getTerminalTabList();
    //    for (mainIndex in menuList) {
    //        var $li;
    //        var subMenuList = menuList[mainIndex].sub_menu_list;
    //        if (null != subMenuList && subMenuList.length > 0) {
    //            $li = $('<li class=\"IndexNavF\" onclick=\"mainMenuClick(this)\"><a href=\"#\">' + menuList[mainIndex].type + '<img src=\"/images/IndexNav.png\" alt=\"\"></a></li>');
    //            $("nav#mainMenuList ul").append($li);
    //            for (subIndex in subMenuList) {
    //                $li = $('<li id=\"menuId_' + subMenuList[subIndex].type + '\" class=\"IndexNavF\" onclick=\"getMenuGoodsInfo(this, ' + menuList[mainIndex].type + ', ' + subMenuList[subIndex].type + ')\"><a href=\"#\">' + subMenuList[subIndex].name + '</a></li>');
    //                $("nav#mainMenuList ul").append($li);
    //            }
    //        } else {
    //            $li = $('<li id=\"menuId_' + menuList[mainIndex].type + '\" class=\"IndexNavF\" onclick=\"getMenuGoodsInfo(this, ' + menuList[mainIndex].type + ', ' + TerminalMgr.invalidTabId + ')\"><a href=\"#\">' + menuList[mainIndex].name + '</a></li>');
    //            $("nav#mainMenuList ul").append($li);
    //        }
    //    }
    //    $("#menuId_" + GoodsMgr.adjustSufKeyFromTab(mainTab, subTab)).addClass("IndexNavFocus");
    //}

    function clickTab(mainTab, subTab) {
        var curMainTab = TerminalMgr.getCurMainTab();
        var curSubTab = TerminalMgr.getCurSubTab();
        if (curMainTab != mainTab || curSubTab != subTab) {
            $("div#goodsList div").each(function () {
                $(this).remove();
            });

            TerminalMgr.setCurTabKey(mainTab, subTab);
            setClickMenu();
            scrollBegin = 0;
            GoodsMgr.getGoodsList(scrollBegin, GoodsMgr.goodsQueryNum, addGoodsList);
        }
    }

    function resetClickMenu() {
        var clickMenuKey = GoodsMgr.adjustSufKeyFromTab(TerminalMgr.getCurMainTab(), TerminalMgr.getCurSubTab());
        $("#menuid_" + clickMenuKey).removeClass("IndexNavFocus");
    }

    function setClickMenu() {
        var clickMenuKey = GoodsMgr.adjustSufKeyFromTab(TerminalMgr.getCurMainTab(), TerminalMgr.getCurSubTab());
        $("#menuid_" + clickMenuKey).addClass("IndexNavFocus");
    }

    function getMenuGoodsInfo(mainTab, subTab) {
        resetClickMenu();
        clickTab(mainTab, subTab);
    }

    //便利店
    function mainMenuClick(obj, mainTab) {
        if ($(obj).next(".IndexNavSon").attr("style") == "display:none") {
            $(".IndexNavSon").attr("style", "display:block");
            $(obj).find("img").attr("src", "/images/IndexNav.png")
        } else {
            $(".IndexNavSon").attr("style", "display:none");
            $(obj).find("img").attr("src", "/images/IndexNavDown.png")
        }

        var subTab = TerminalMgr.getCurSubTab();
        if (TerminalMgr.invalidTabId == subTab) {
            resetClickMenu();
            subTab = TerminalMgr.getCurSubTabFromMainTab(mainTab);
        }
        clickTab(mainTab, subTab);
    }

    function isScroll(scrollWindow, scrollDoc, scrollCallBack) {
        $(scrollWindow).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var windowHeight = $(this).height();
            var docHeight = $(scrollDoc).height();
            if (scrollTop >= (docHeight - windowHeight)) {
                if (scrollCallBack) {
                    scrollCallBack();
                };
            }
        });
    }

    function addGoodsList(goodsList) {
        if (null != goodsList && (scrollBegin < goodsList.length)) {
            var curMainTab = TerminalMgr.getCurMainTab();
            if (curMainTab == TerminalMgr.promotionTabId) {
                addPromotionGoodsList(goodsList);
            } else if (curMainTab == TerminalMgr.conveningStoreTabId) {
                addStoreGoodsList(goodsList);
                adjustGoodsList();
            }
            scrollBegin = goodsList.length;
        }
    }

    function addPromotionGoodsList(goodsList) {
        for (var i = scrollBegin; i < goodsList.length; i++) {
            var $div = $(
                '<div class=\"IndexPart\">'
                +'<a class=\"count_' + goodsList[i].goods_info.goodsid +'\" goodsid=\"' +goodsList[i].goods_info.goodsid +'\" images=\"' + goodsList[i].goods_info.image + '\" onclick=\"promotionGoods(' + goodsList[i].goods_info.goodsid + ',' + goodsList[i].main_tab + ',' + goodsList[i].sub_tab + ');\">' +
                '<img class=\"fll\" style=\"width:100%\" src=\"' + goodsList[i].goods_info.image + '\">' +
                '</a>' +
                '</div>');
            $("div#goodsList").append($div);
        }
    }

    function addStoreGoodsList(goodsList) {
        for (var i = scrollBegin; i < goodsList.length; i++) {
            var $div = $(
            '<div class=\"IndexPart\">'
            + '<div class=\"IndexGoodsImg\">'
            + '<img class=\"GoodsImg\" src=\"' + goodsList[i].goods_info.image +'\">'
            + '</div>'
            + '<div class=\"IndexGoodsTxt\">'
            + '<h1>' + goodsList[i].goods_info.name + '</h1>'
            + '</div>'
            + '<section>'
            + '<span>￥' + goodsList[i].goods_info.price + '</span>'
            + '<div class=\"IndexGoodsNum\">'
            + '<a class=\"dec_count_' + goodsList[i].goods_info.goodsid + '\" onclick=\"adjustNum1(' + goodsList[i].goods_info.goodsid + ', ' + goodsList[i].main_tab + ', ' + goodsList[i].sub_tab + ', -1)\"><img src=\"/images/Dec.png\"></a>'
            + '<span id=\"goodsid_' + goodsList[i].goods_info.goodsid + '\">' + goodsList[i].goods_num + '</span>'
            + '<a class=\"add_count_' + goodsList[i].goods_info.goodsid + '\" onclick=\"adjustNum1(' + goodsList[i].goods_info.goodsid + ', ' + goodsList[i].main_tab + ', ' + goodsList[i].sub_tab + ', 1)\"><img src=\"/images/Add.png\"></a>'
            + '</div>'
            + '</section>'
            + '</div>');
            $("div#goodsList").append($div);
        }
    }

    function setGoodsListScrollListener() {
        var goodsWindow = $(".goodsWindow");
        var goodsDoc = $(".IndexGoods");
        isScroll(goodsWindow, goodsDoc, function () {
            scrollBegin = GoodsMgr.getCurTagGoodsNum();
            if (scrollBegin > 0) {//滚动都是来自已有列表，如果是空列表，就不触发这个事件，由click载入
                GoodsMgr.getGoodsList(scrollBegin, GoodsMgr.goodsQueryNum, addGoodsList);
            }
        });
    }

    function adjustGoodsList() {
        var goodsList = GoodsMgr.getCurTabGoodsList();
        $('.IndexPart .IndexGoodsNum').each(function (index) {
            var goodsNum = Number(goodsList[index].goods_num);
            if (goodsNum <= 0) {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).hide();
                $('.IndexPart .IndexGoodsNum span').eq(index).hide();
            } else {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).show();
                $('.IndexPart .IndexGoodsNum span').eq(index).show();
            }
        });
    }

    function promotionGoods(goodsId, mainTab, subTab) {
        var goodsInfo = GoodsMgr.getGoodsInfoFromLocale(goodsId, mainTab, subTab);
        PromotionMgr.setPromotionGoodsInfo(goodsInfo);
        var url = '@Url.Content("~/")' + 'Promotion/Index';
        window.location.href = url;
    }

    function adjustGoodsListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_count_" + goodsid).show();
            $("#goodsid_" + goodsid).show();
            $("#goodsid_" + goodsid).text(goodsNum);
        } else {
            $(".dec_count_" + goodsid).hide();
            $("#goodsid_" + goodsid).hide();
        }
    }
	//函数名末尾的1是为了与Shopping页面中的函数名区分开来
    function adjustGoodsNumShow1(goodsid, goodsNum, shoppingList) {
        adjustGoodsListShow(goodsid, goodsNum);

        var url = '@Url.Content("~/")' + 'Home/Shopping';
        NativeAjax(shoppingList, url, function (msg) {
            loadPartialViewResult(msg, $("#shopping"), 1);
        });
    }
	//函数名末尾的1是为了与Shopping页面中的函数名区分开来
    function adjustNum1(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustGoodsNumShow1);
    }
</script>